﻿<?php

$dbhost='10.11.221.22';
$dbname='zabbix';
$dbuser='kapb14';
$dbpass='rfhsx3415';

$link=mysql_connect($dbhost,$dbuser,$dbpass) or die(mysql_errno($link).mysql_error($link));
$db=mysql_select_db($dbname,$link) or die(mysql_errno($link).mysql_error($link));


$hostid = $_GET['hostid'];
$search_query = $_POST["search_query"];

/*	последние 20 событий -- НАЧАЛО */

$last20events_q = "
SELECT FROM_UNIXTIME(e.clock) as time, e.value, TIMEDIFF(NOW(), FROM_UNIXTIME(e.clock)) as timedif
FROM zabbix.events e 
WHERE e.objectid IN 	(
			SELECT DISTINCT	events.objectid
			FROM triggers triggers 
				INNER JOIN events 		events ON triggers.triggerid = events.objectid
				INNER JOIN functions 	functions ON triggers.triggerid = functions.triggerid
				INNER JOIN items 		items ON functions.itemid = items.itemid
				INNER JOIN hosts 		hosts ON items.hostid = hosts.hostid
			WHERE
				hosts.hostid = '$hostid'
				AND triggers.description = 'down'
			)
ORDER BY e.clock DESC LIMIT 20";
$last20events_r=mysql_query($last20events_q, $link) or die(mysql_errno($link).mysql_error($link));
/*	последние 20 событий -- КОНЕЦ */

/*	форма поиска -- НАЧАЛО */

$search_query_q = "
SELECT
    hosts.hostid, hosts.host, hosts.ip, hosts_profiles.location, hosts_profiles.contact
FROM
    zabbix.hosts INNER JOIN zabbix.hosts_profiles ON (hosts.hostid = hosts_profiles.hostid)
WHERE 		hosts.host LIKE '%$search_query%'
		OR 	hosts.ip LIKE '%$search_query%'
		OR	hosts_profiles.location LIKE '%$search_query%'
		OR 	hosts_profiles.contact LIKE '%$search_query%' 
ORDER BY hosts.host DESC";
$search_query_r=mysql_query($search_query_q, $link) or die(mysql_errno($link).mysql_error($link));
$search_query_rch = mysql_num_rows($search_query_r);
if ($search_query_rch == 0){
	echo "Сори, строка поиска ($search_query) не найдена в базе. Попробуйте как-то иначе..";
	exit; 
	}
	else if ($search_query_rch == 1)
	{
		echo "Найдено <b>1</b> совпадение!<br>";
		}
	else {
		echo "Найдено <b>$search_query_rch</b> совпадений! <br>";
		}
/*	форма поиска -- КОНЕЦ */

?>
